package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tuna.UserOp;

/**
 * Servlet implementation class proceedRating
 */
public class proceedRating extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		
		try {
			String utorId = request.getParameter("utorId");
			String userId = request.getParameter("userId");
			String profId = request.getParameter("profId");
			String pres_val = request.getParameter("pres");
			String comm_val = request.getParameter("comm");
			String exp_val = request.getParameter("exp");
			String teach_val = request.getParameter("teach");
			String work_val = request.getParameter("work");
			String diff_val = request.getParameter("diff");
			String learn_val = request.getParameter("learn");
						
			if (utorId == null || utorId.isEmpty() || profId == null || profId.isEmpty() ||
					pres_val == null || pres_val.isEmpty() || pres_val == null || pres_val.isEmpty() ||
					comm_val == null || comm_val.isEmpty() || exp_val == null || exp_val.isEmpty() ||
					teach_val == null || teach_val.isEmpty() || work_val == null || work_val.isEmpty() ||
					diff_val == null || diff_val.isEmpty() || learn_val == null || learn_val.isEmpty() ||
					userId == null || userId.isEmpty()) {
				response.setContentType("text/xml");
				out.print("<message>Error: One of parameters is missed</message>");
				return;
			}
			
			String message;
			
			int intId = Integer.parseInt(userId);
			
			UserOp user = new UserOp(utorId, intId);
			if(user.submitRating(userId, profId, pres_val, comm_val, 
					exp_val, teach_val, work_val, learn_val)) {
				message = "Rating updated successfully";
			}
			else {
				message = "Error: Exception at SQL level";
			}
			
			response.setContentType("text/xml");
			out.print("<response>");
			out.print("<message>" + message + "</message>");
			out.print("</response>");
		}
		catch (Exception ex) {
			response.setContentType("text/xml");
			out.print("<message>Error: Exception</message>");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
